﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;

namespace phoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        ServiceReference1.Service1Client client;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            client = new ServiceReference1.Service1Client();
            client.OpenAsync();
            client.validCredentialsCompleted += new EventHandler
                    <ServiceReference1.validCredentialsCompletedEventArgs>(client_validCredentialsCompleted);
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            String login = loginTextbox.Text;
            String password = passwordTextbox.Text;

            if (login.Equals(""))
            {
                loginTextbox.Background = new SolidColorBrush(Colors.Red);
            }

            if (password.Equals(""))
            {
                passwordTextbox.Background = new SolidColorBrush(Colors.Red);
            }

            if (!login.Equals("") && !password.Equals(""))
            {
                WcfConnection wcfConnection = new WcfConnection();

                client.validCredentialsAsync(login, password);
                MessageBox.Show("czekaj na zalogowanie.");
                
            }
        }

        private void client_validCredentialsCompleted(object sender, ServiceReference1.validCredentialsCompletedEventArgs e)
        {
            if (e.Result)
            {
                var alarm = new Alarm("alarm1"+DateTime.Now.ToFileTimeUtc());
                alarm.Content = "zalogowales sie do aplikacji";

                alarm.BeginTime = DateTime.Now.AddSeconds(5);
                alarm.ExpirationTime = alarm.BeginTime.AddMinutes(5);

                ScheduledActionService.Add(alarm);
                
                NavigationService.Navigate(new Uri("/MenuPage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("zly login.");
            }
        }

        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/RegistrationPage.xaml", UriKind.Relative));
        }
    }
}